package com.haochang.chunkvod.controller.adapter.hostess;

import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SoundPoolUtil;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.VibratorUtil;
import com.haochang.chunkvod.controller.activity.hostess.DeskManagerActivity;
import com.haochang.chunkvod.controller.activity.hostess.HostessHomeActivity;
import com.haochang.chunkvod.controller.activity.hostess.RoomManagerActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.BaseInfo;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.DrawableUtils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

/**
 * 咨客首页适配器
 */
public class HostessHomePageAdapter extends QuickAdapter<HostessInfo>
{
	private int keyOfTag = R.string.app_name;
	private Context mContext;
	private List<BaseInfo> baseInfoList;//颜色配置
	private Animation alphaAnimation;//透明度动画,全局变量可以保证动画同步
	private SparseArray<View> sparseArray = new SparseArray<View>();//保存具有闪烁功能的view的引用
	private boolean hasAnimation;//是否有动画

	public HostessHomePageAdapter(Context context, int layoutResId, List<HostessInfo> data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	public void setBaseInfoList(List<BaseInfo> baseInfoList)
	{
		this.baseInfoList = baseInfoList;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final HostessInfo item)
	{
		final BaseTextView tv_roomStatus = helper.getView(R.id.tv_roomStatus);
		helper.setText(R.id.tv_remark, !TextUtils.isEmpty(item.getRemarks()) ? item.getRemarks() : "");
		helper.setText(R.id.tv_roomNum, item.getRoomNum());
		helper.getView().setTag(keyOfTag, item);
		helper.getView().setOnClickListener(clickListener);
		helper.setText(R.id.tv_time,
				TextUtils.isEmpty(item.getStart_time()) ? TimeUtils.getMinutesString(item.getEnd_time())
						: TimeUtils.getMinutesString(item.getStart_time()) + "-"
								+ (TextUtils.isEmpty(item.getEnd_time()) ? "**" : TimeUtils.getMinutesString(item.getEnd_time())));
		if (0 == item.getClassifyType())
		{//以下为房间{
			helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);//隐藏批次号
			helper.getView(R.id.tv_room_type_count).setVisibility(View.VISIBLE);
			if (item.getCustomerCount() == 0)
			{
				helper.setText(R.id.tv_room_type_count, item.getRoomType());
			} else
			{
				helper.setText(R.id.tv_room_type_count, item.getRoomType() + "(" + item.getCustomerCount() + ")");
			}
			if (item.getIsHonor() > 0)
			{//房间大客户
				helper.getView(R.id.img_shopping).setVisibility(View.VISIBLE);
				helper.getView(R.id.img_shopping).setBackgroundResource(R.drawable.big_status);
			} else
			{
				helper.getView(R.id.img_shopping).setVisibility(View.GONE);
			}
			//			setRoomStyle(item.getRoomStatus(), item.getUsage(), item.getUnLeave(), helper.getView(), tv_roomStatus);
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.setDefaultRoomStyle(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setRoomStyleByService(mContext, item.getRoomStatus(), item.getUsage(), helper.getView(), tv_roomStatus, baseInfoList);
			//以上为房间}
		} else if (1 == item.getClassifyType())
		{//以下为桌台{
			helper.getView(R.id.img_shopping).setVisibility(View.GONE);//隐藏房间大客户图标
			if (item.getCustomerCount() == 0)
			{
				helper.getView(R.id.tv_room_type_count).setVisibility(View.GONE);
			} else
			{
				helper.getView(R.id.tv_room_type_count).setVisibility(View.VISIBLE);
				helper.setText(R.id.tv_room_type_count, "(" + item.getCustomerCount() + ")");
			}
			if (item.getAbsNumber() == 0)
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);
			} else
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.VISIBLE);
				helper.setText(R.id.tv_batch_num, "(+" + item.getAbsNumber() + ")");
			}
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.settDefaultDeskStyle(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setDeskStyleByService(mContext, item.getRoomStatus(), helper.getView(), tv_roomStatus, baseInfoList);
			//以上为桌台}
		}

		//闪烁测试
		item.setFlickering(helper.getPosition() % 4 == 0);
		if (item.isFlickering())
		{
			if (!hasAnimation)
			{
				VibratorUtil.getInstance().startVibrate(context, true);
				SoundPoolUtil.getInstance().startPlay(context);
			}
			hasAnimation = true;
			sparseArray.put(helper.getPosition(), helper.getView());
			startFlick(helper.getView());
		} else
			stopFlick(helper.getView());

	}

	/**
	 * 动画开启闪烁
	 *
	 * @param view getView的view
	 */
	private void startFlick(View view)
	{
		if (null == view)
			return;
		if (null == alphaAnimation)
		{
			alphaAnimation = new AlphaAnimation(1, 0.2f);
			alphaAnimation.setDuration(500);
			alphaAnimation.setInterpolator(new LinearInterpolator());
			alphaAnimation.setRepeatCount(Animation.INFINITE);
			alphaAnimation.setRepeatMode(Animation.REVERSE);
		}
		view.startAnimation(alphaAnimation);
	}

	/**
	 * 清除闪烁动画
	 *
	 * @param view getView的view
	 */
	private void stopFlick(View view)
	{
		if (null == view)
			return;
		view.clearAnimation();
	}

	/**
	 * 清除所有闪烁效果
	 */
	public void clearAllAnimation()
	{
		if (sparseArray.size() > 0)
		{
			int size = sparseArray.size();
			for (int i = 0; i < size; i++)
			{
				View view = sparseArray.get(sparseArray.keyAt(i));
				stopFlick(view);
			}
			sparseArray.clear();
		}
	}

	/**
	 * 设置adapter是否包含闪烁动画
	 */
	public void setHasAnimation(boolean hasAnimation)
	{
		this.hasAnimation = hasAnimation;
	}

	/**
	 * 开始震动和播放声音 ,动画启动在adapter-getview里面，这里就不用再调用
	 */
	public void startVibratorAndVoice()
	{
		if (hasAnimation)
		{
			VibratorUtil.getInstance().startVibrate(context, true);//开始重复震动
			SoundPoolUtil.getInstance().startPlay(context);//开始播放声音
		}
	}

	/**
	 * 停止震动和停止播放声音 ,消除动画
	 */
	public void stopVibratorAndVoice()
	{
		VibratorUtil.getInstance().stopVibrate();//消除震动
		SoundPoolUtil.getInstance().stopPlay();//消除声音
	}

	private OnBaseClickListener clickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			HostessInfo info = (HostessInfo) v.getTag(keyOfTag);
			if (null != info)
			{
				Intent intent = new Intent(mContext, info.getClassifyType() == 0 ? RoomManagerActivity.class : DeskManagerActivity.class);
				intent.putExtra(IntentKey.ROOM_NUM, info.getRoomNum());
				intent.putExtra(IntentKey.ROOM_ID, info.getRoomId());
				intent.putExtra(IntentKey.ROOM_STATUS, info.getRoomStatus());
				intent.putExtra(IntentKey.ROOM_USAGE, info.getUsage());
				intent.putExtra(IntentKey.BILL_ID, info.getBillId());
				intent.putExtra(IntentKey.ROOM_TYPE_ID, info.getRoomTypeId());
				intent.putExtra(IntentKey.ROOM_TYPE, info.getRoomType());
				intent.putExtra(IntentKey.COMSTMER_COUNT, info.getCustomerCount() + "");
				intent.putExtra(IntentKey.REMARKS, info.getRemarks() + "");
				((HostessHomeActivity) mContext).startActivityForResult(intent, IntentCode.HOSTESS_MANAGER);
			}
		}
	};
}
